/* 
 * File:   BMPHeader.cpp
 * Author: mm021791
 * 
 * Created on 3 de octubre de 2012, 09:43
 */

#include <unistd.h>

#include "BMPHeader.h"

using namespace TP::Grafica::Renderizado;

BMPHeader::BMPHeader() {}

BMPHeader::BMPHeader
(
    unsigned short int type,
    long  size,
    unsigned short int reserve1,
    unsigned short int reserve2,
    long offBits,                     
    long bmpSize,
    long bmpWidth,
    long bmpHeight,
    unsigned short int bmpPlanes,
    unsigned short int bmpBitCount,  
    long bmpCompression,
    long bmpSizeImage,
    long bmpXPelsPerMeter,
    long bmpYPelsPerMeter,
    long bmpClrUsed,
    long bmpClrImportant             
)
{
    this->type = type;
    this->size = size;
    this->reserve1 = reserve1;
    this->reserve2 = reserve2;
    this->offBits = offBits;                     
    this->bmpSize = bmpSize;
    this->bmpWidth = bmpWidth;
    this->bmpHeight = bmpHeight;
    this->bmpPlanes = bmpPlanes;
    this->bmpBitCount = bmpBitCount;  
    this->bmpCompression = bmpCompression;
    this->bmpSizeImage = bmpSizeImage;
    this->bmpXPelsPerMeter = bmpXPelsPerMeter;
    this->bmpYPelsPerMeter = bmpYPelsPerMeter;
    this->bmpClrUsed = bmpClrUsed;
    this->bmpClrImportant = bmpClrImportant;
}

    
BMPHeader::~BMPHeader() {}

namespace TP{ namespace Grafica { namespace Renderizado { 
    
    istream& operator>> (istream &stream, BMPHeader &header){

        stream.seekg(0, ios::beg);
        
        // File Header.
        unsigned short int u_2byte;
        long int u_4byte;

        stream.read(reinterpret_cast < char * > (&u_2byte), sizeof (u_2byte));
        header.SetType(u_2byte);

        stream.read(reinterpret_cast < char * > (&u_4byte), sizeof (u_4byte));
        header.SetSize(u_4byte);

        stream.read(reinterpret_cast < char * > (&u_2byte), sizeof (u_2byte));
        header.SetReserve1(u_2byte);

        stream.read(reinterpret_cast < char * > (&u_2byte), sizeof (u_2byte));
        header.SetReserve2(u_2byte);

        stream.read(reinterpret_cast < char * > (&u_4byte), sizeof (u_4byte));
        header.SetOffBits(u_4byte);

        // Image Header.

        stream.read(reinterpret_cast < char * > (&u_4byte), sizeof (u_4byte));
        header.SetBmpSize(u_4byte);

        stream.read(reinterpret_cast < char * > (&u_4byte), sizeof (u_4byte));
        header.SetBmpWidth(u_4byte);

        stream.read(reinterpret_cast < char * > (&u_4byte), sizeof (u_4byte));
        header.SetBmpHeight(u_4byte);

        stream.read(reinterpret_cast < char * > (&u_2byte), sizeof (u_2byte));
        header.SetBmpPlanes(u_2byte);

        stream.read(reinterpret_cast < char * > (&u_2byte), sizeof (u_2byte));
        header.SetBmpBitCount(u_2byte);

        stream.read(reinterpret_cast < char * > (&u_4byte), sizeof (u_4byte));
        header.SetBmpCompression(u_4byte);

        stream.read(reinterpret_cast < char * > (&u_4byte), sizeof (u_4byte));
        header.SetBmpSizeImage(u_4byte);

        stream.read(reinterpret_cast < char * > (&u_4byte), sizeof (u_4byte));
        header.SetBmpXPelsPerMeter(u_4byte);

        stream.read(reinterpret_cast < char * > (&u_4byte), sizeof (u_4byte));
        header.SetBmpYPelsPerMeter(u_4byte);

        stream.read(reinterpret_cast < char * > (&u_4byte), sizeof (u_4byte));
        header.SetBmpClrUsed(u_4byte);

        stream.read(reinterpret_cast < char * > (&u_4byte), sizeof (u_4byte));
        header.SetBmpClrImportant(u_4byte);

        return stream;
    }

    ostream& operator<<(ostream &stream, const BMPHeader& header){
        
        stream.seekp(0);
        
        // Info de archivo.
        stream.write(reinterpret_cast<const char*>(&header.type), sizeof (header.type) );
        stream.write(reinterpret_cast<const char*>(&header.size), sizeof (header.size) );
        stream.write(reinterpret_cast<const char*>(&header.reserve1), sizeof (header.reserve1) );
        stream.write(reinterpret_cast<const char*>(&header.reserve2), sizeof (header.reserve2) );
        stream.write(reinterpret_cast<const char*>(&header.offBits), sizeof (header.offBits) );

        // Info de imagen.
        stream.write(reinterpret_cast<const char*>(&header.bmpSize), sizeof (header.bmpSize) );
        stream.write(reinterpret_cast<const char*>(&header.bmpWidth), sizeof (header.bmpWidth) );
        stream.write(reinterpret_cast<const char*>(&header.bmpHeight), sizeof (header.bmpHeight) );
        stream.write(reinterpret_cast<const char*>(&header.bmpPlanes), sizeof (header.bmpPlanes) );
        stream.write(reinterpret_cast<const char*>(&header.bmpBitCount), sizeof (header.bmpBitCount) );
        stream.write(reinterpret_cast<const char*>(&header.bmpCompression), sizeof (header.bmpCompression) );
        stream.write(reinterpret_cast<const char*>(&header.bmpSizeImage), sizeof (header.bmpSizeImage) );
        stream.write(reinterpret_cast<const char*>(&header.bmpXPelsPerMeter), sizeof (header.bmpXPelsPerMeter) );
        stream.write(reinterpret_cast<const char*>(&header.bmpYPelsPerMeter), sizeof (header.bmpYPelsPerMeter) );
        stream.write(reinterpret_cast<const char*>(&header.bmpClrUsed), sizeof (header.bmpClrUsed) );
        stream.write(reinterpret_cast<const char*>(&header.bmpClrImportant), sizeof (header.bmpClrImportant) );
        
        return stream;
    }

}}}


